const cloud = require('wx-server-sdk')
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()

exports.main = async (event, context) => {
    try {
        // 获取所有文档ID
        const { data } = await db.collection('case_resume_template')
            .field({ _id: true })
            .get()

        // 批量更新设置
        const batchSize = 100
        const promises = []

        for (let i = 0; i < data.length; i += batchSize) {
            const batch = data.slice(i, i + batchSize)
            promises.push(
                db.collection('case_resume_template')
                    .where({
                        _id: db.command.in(batch.map(item => item._id))
                    })
                    .update({
                        data: {
                            views: 100, // 默认浏览量0
                            likes: 100  // 默认点赞数0
                        }
                    })
            )
        }

        const results = await Promise.all(promises)
        const updated = results.reduce((sum, r) => sum + r.stats.updated, 0)

        return {
            code: 0,
            message: `成功更新${updated}条文档`,
            updatedCount: updated
        }
    } catch (err) {
        console.error('批量更新失败:', err)
        return {
            code: -1,
            message: err.message
        }
    }
}